using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using UnityEngine;

namespace BCW.Friends
{
	public class FriendsUIController : SelectableButtonController
	{
		public enum FriendUITab
		{
			Friends = 0,
			Invites = 1,
			Search = 2,
			Chat = 3,
			None = 4
		}

		public static FriendsUIController instance;

		public static FriendUITab currentTab = FriendUITab.None;

		public GameObject Holder;

		public FriendsUICategory TabFriends;

		public FriendsUICategory TabSearch;

		public FriendsUIChat TabChat;

		public FriendsUIJoinMessage messageWindow;

		public GameObject UpdateVersionMessageHolder;

		public FriendsUICountFriendsLable lableCountFriends;

		private bool firstOpen = true;

		public FriendInfoUI friendToRemove;

		private void Awake()
		{
			instance = this;
			allButtons.AddRange(GetComponentsInChildren<FriendsUITabButton>(true));
			FriendsController.OnIdsListsUpdated += UpdateButtons;
		}

		private void OnEnable()
		{
		}

		private void OnDestroy()
		{
			instance = null;
			FriendsController.OnIdsListsUpdated -= UpdateButtons;
		}

		public void OpenTab(FriendUITab tab)
		{
			TabChat.gameObject.SetActive(false);
			TabSearch.gameObject.SetActive(false);
			TabFriends.gameObject.SetActive(tab == FriendUITab.Friends || tab == FriendUITab.Invites);
			TabSearch.ClearAllButtons();
			TabFriends.ClearAllButtons();
			switch (tab)
			{
			case FriendUITab.Friends:
			{
				List<PlayerInfo> listForIds = GetListForIds(FriendsController.instance.FriendsIdsWithFake());
				TabFriends.Init(listForIds);
				FriendsUIMessageManager.instance.OnFriendsTabOpen(listForIds.Count);
				FriendsController.instance.CountNewFriends = 0u;
				break;
			}
			case FriendUITab.Invites:
			{
				List<PlayerInfo> listForIds = GetListForIds(FriendsController.instance.InvitesIds);
				TabFriends.Init(listForIds);
				FriendsUIMessageManager.instance.OnInvitesTabOpen(listForIds.Count);
				break;
			}
			case FriendUITab.Chat:
				TabChat.gameObject.SetActive(true);
				break;
			case FriendUITab.Search:
			{
				TabSearch.gameObject.SetActive(true);
				TabSearch.findWebPlayer = false;
				List<PlayerInfo> listForIds = GetListForIds(FindFriendsFromLocalLAN.lanPlayerInfo);
				TabSearch.Init(listForIds);
				FriendsUIMessageManager.instance.OnSearchTabOpen(listForIds.Count);
				break;
			}
			}
			currentTab = tab;
		}

		public void EnableAllTabsButtons()
		{
			TabFriends.allButtons.ForEach(delegate(SelectableButton x)
			{
				(x as FriendInfoUI).canBeSelected = true;
			});
			TabSearch.allButtons.ForEach(delegate(SelectableButton x)
			{
				(x as FriendInfoUI).canBeSelected = true;
			});
		}

		private List<PlayerInfo> GetListForIds(IList<string> ids)
		{
			List<PlayerInfo> list = new List<PlayerInfo>();
			List<string> list2 = new List<string>();
			Dictionary<string, PlayerInfo> playerDBWithFake = FriendsController.instance.getPlayerDBWithFake();
			foreach (string id in ids)
			{
				if (playerDBWithFake.ContainsKey(id))
				{
					list.Add(playerDBWithFake[id]);
				}
				else
				{
					list2.Add(id);
				}
			}
			if (list2.Count > 0)
			{
				FriendsController.instance.RequestPlayerInfo(list2, UpdateButtons);
			}
			return list;
		}

		public void ReInitSearchTab()
		{
			if (currentTab != FriendUITab.Search || TabSearch.findWebPlayer)
			{
				return;
			}
			List<string> list = new List<string>();
			foreach (string item in FindFriendsFromLocalLAN.lanPlayerInfo)
			{
				bool flag = false;
				foreach (SelectableButton allButton in TabSearch.allButtons)
				{
					if ((allButton as FriendInfoUI).pInfo.playerId.Equals(item))
					{
						flag = true;
						break;
					}
				}
				if (!flag)
				{
					list.Add(item);
				}
			}
			TabSearch.Init(GetListForIds(list));
		}

		private void UpdateButtons()
		{
			if (!Holder.activeSelf)
			{
				return;
			}
			ReadOnlyCollection<string> currentList;
			switch (currentTab)
			{
			default:
				return;
			case FriendUITab.Friends:
				currentList = FriendsController.instance.FriendsIds;
				FriendsUIMessageManager.instance.OnFriendsTabOpen(currentList.Count);
				break;
			case FriendUITab.Invites:
				currentList = FriendsController.instance.InvitesIds;
				FriendsUIMessageManager.instance.OnInvitesTabOpen(currentList.Count);
				break;
			}
			List<SelectableButton> removeList = new List<SelectableButton>();
			List<string> list = new List<string>();
			TabFriends.allButtons.ForEach(delegate(SelectableButton x)
			{
				if (!currentList.Contains((x as FriendInfoUI).pInfo.playerId))
				{
					removeList.Add(x);
				}
			});
			foreach (string item in currentList)
			{
				bool flag = false;
				foreach (SelectableButton allButton in TabFriends.allButtons)
				{
					if ((allButton as FriendInfoUI).pInfo.playerId.Equals(item))
					{
						flag = true;
						break;
					}
				}
				if (!flag)
				{
					list.Add(item);
				}
			}
			if (list.Count > 0)
			{
				TabFriends.Init(GetListForIds(list));
			}
			if (removeList.Count > 0)
			{
				TabFriends.RemoveButtons(removeList);
			}
		}

		public void Show()
		{
			if (CheckVersion.instance != null && !CheckVersion.actualVersion)
			{
				CheckVersion.instance.ShowUpdate();
				return;
			}
			if (firstOpen)
			{
				Invoke("ResizeScroll", 0.3f);
				lableCountFriends.gameObject.SetActive(true);
			}
			Holder.SetActive(true);
			GameUI.instance.gameObject.SetActive(false);
			if (firstOpen && FriendsController.instance.playerDB.Count == 0)
			{
				LoadingPanel.instance.ShowLoadingPanel(true);
				FriendsController.instance.UpdatePlayersDB(delegate
				{
					SelectButton(allButtons.Find((SelectableButton x) => (x as FriendsUITabButton).tabType == FriendUITab.Friends));
					LoadingPanel.instance.ShowLoadingPanel(false);
					Invoke("ResizeScroll", 0.6f);
					firstOpen = false;
					lableCountFriends.gameObject.SetActive(true);
				});
			}
			else
			{
				SelectButton(allButtons.Find((SelectableButton x) => (x as FriendsUITabButton).tabType == FriendUITab.Friends));
				FriendsController.instance.UpdatePlayersDB(null);
			}
			foreach (SelectableButton allButton in allButtons)
			{
				switch ((allButton as FriendsUITabButton).tabType)
				{
				case FriendUITab.Chat:
					(allButton as FriendsUITabButton).ShowNotification(FriendsController.instance.CountNewMessages);
					break;
				case FriendUITab.Friends:
					(allButton as FriendsUITabButton).ShowNotification(FriendsController.instance.CountNewFriends);
					break;
				case FriendUITab.Invites:
					(allButton as FriendsUITabButton).ShowNotification((uint)FriendsController.instance.InvitesIds.Count);
					break;
				case FriendUITab.Search:
					(allButton as FriendsUITabButton).ShowNotification(0u);
					break;
				}
			}
		}

		public void UIRemovePlayer()
		{
			friendToRemove.RemoveFriend();
		}

		public void UICancelRemovePlayer()
		{
			EnableAllTabsButtons();
			friendToRemove.UICancelRemove();
		}

		private void ResizeScroll()
		{
			float width = TabFriends.grid.transform.parent.gameObject.GetComponent<UIPanel>().width;
			int maxPerLine = Mathf.FloorToInt(width / TabFriends.grid.cellWidth);
			TabFriends.grid.maxPerLine = maxPerLine;
			TabSearch.grid.maxPerLine = maxPerLine;
			TabFriends.grid.repositionNow = true;
			TabSearch.grid.repositionNow = true;
		}

		public void Hide()
		{
			currentTab = FriendUITab.None;
			GameUI.instance.gameObject.SetActive(true);
			Holder.SetActive(false);
			TabFriends.ClearAllButtons();
			foreach (SelectableButton allButton in allButtons)
			{
				allButton.Select(false);
			}
			if (FriendsController.instance != null)
			{
				FriendsController.instance.RemoveNewStatus();
			}
			selectedButton = null;
		}

		public void SeletctButtonByType(FriendUITab buttontype)
		{
			SelectButton(allButtons.Find((SelectableButton x) => (x as FriendsUITabButton).tabType == buttontype));
		}

		public void SortGrid<T>(UIGrid grid, Comparison<T> comparator)
		{
			if (grid == null)
			{
				return;
			}
			grid.sorting = UIGrid.Sorting.Alphabetic;
			List<Transform> list = (from x in grid.GetChildList()
				where x.gameObject.activeSelf
				select x).ToList();
			Transform transform = null;
			for (int i = 0; i < list.Count; i++)
			{
				for (int j = 0; j < list.Count - 1; j++)
				{
					if (comparator(list[j].GetComponent<T>(), list[j + 1].GetComponent<T>()) == 1)
					{
						transform = list[j + 1];
						list[j + 1] = list[j];
						list[j] = transform;
					}
				}
			}
			for (int k = 0; k < list.Count; k++)
			{
				string text = string.Empty;
				if (k < 10)
				{
					text = "00";
				}
				else if (k < 100)
				{
					text = "0";
				}
				text += k;
				list[k].gameObject.name = text;
			}
			grid.repositionNow = true;
		}
	}
}
